<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
    <!-- 引入分页导航文件 -->
    <script src="./libs/jquery.twbsPagination.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option>
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="row text-center">
                <!-- 准备分页导航容器 -->
                <ul id="pagination" class="pagination-sm"></ul>
            </div>
        </div>
    </div>
</body>

</html>
<script src="./libs/https.js"></script>
<!-- 文章类别模板引擎 -->
<script type="text/html" id="tpl-category">
    <option value="">所有分类</option>
    {{each}}
    <option value="{{$value.id}}">{{$value.name}}</option>
    {{/each}}
</script>
<!-- 文章列表模板引擎 -->
<script type="text/html" id="tpl-list">
    {{each}}
    <tr>
        <td>{{$value.title}}</td>
        <td>{{$value.author}}</td>
        <td>{{$value.category}}</td>
        <td class="text-center">{{$value.date}}</td>
        <td class="text-center">{{$value.state}}</td>
        <td class="text-center">
            <a href="article_edit.html?dataID={{$value.id}}" dataID="{{$value.id}}" class="btn btn-default btn-xs">编辑</a>
            <a href="javascript:void(0);" dataID="{{$value.id}}" class="btn btn-danger btn-xs delete">删除</a>
        </td>
    </tr>
    {{/each}}
</script>
<script>
    $(function () {
        // 显示文章分类下拉列表
        $.ajax({
            type: 'get',
            url: BigNew.category_list,
            success: function (response) {
                // console.log(response);
                if (response.code == 200) {
                    let data = response.data
                    let htmlStr = template('tpl-category', data)
                    $('#selCategory').html(htmlStr)

                }

            }
        })

        let selPage=1
        // 封装文章列表函数
        function loadData(page) {
            $.ajax({
                type: 'get',
                url: BigNew.article_query,
                data: {
                    type: $('#selCategory').val(),
                    state: $('#selStatus').val(),
                    page: page,
                    perpage: 11

                },
                success: function (response) {
                    console.log(response);
                    if (response.code == 200) {
                        // 判断是否有数据，没有则正常显示，没有则清空数据并提示信息！
                        if(response.data.totalCount==0){
                            $('tbody').empty()
                            $('#pagination').html('sorry！老鼠来了都流泪！！！')
                            return 
                        }
                        let data = response.data.data
                        let htmlStr = template('tpl-list', data)
                        $('tbody').html(htmlStr)
                        // 设置分页导航之前要先摧毁之前的分页导航
                        $('#pagination').twbsPagination('destroy')
                        // 设置分页导航功能
                        $('#pagination').twbsPagination({
                            totalPages: response.data.totalPage ,
                            visiblePages: 6,
                            // 设置第一次加载分页导航显示的页码
                            startPage:page,
                            // 阻止分页导航初始化，出现递归
                            initiateStartPageClick:false,
                            first:'首页',
                            last:'尾页',
                            next:'下一页',
                            prev:'上一页',
                            onPageClick:function(e,page){
                                // console.log(page);
                                selPage=page
                                loadData(page)
                            }
                           

                        });
                        $('tbody').on('click','.delete',function(){
                                // 1.在哪一页删除完就停留在那一页
                                // 2.如果那一页只有一条数据删完应该去上一页
                                let dataID=$('.delete').attr('dataID')
                          $.ajax({
                              type: 'post',
                              url: BigNew.article_delete,
                              data: {
                                  id:dataID
                              },
                              success: function(response) {
                          console.log(response);      // 3.
                            if(response.code==204){
                                if($('tbody tr').length>1 || selPage==1){
                                loadData(selPage)
                            }
                            else{
                                loadData(--selPage)
                            }
                            }
                                  
                              }
                          })
                            })
                    }

                }
            })
        }
        // 首次加载显示
        loadData()
        // 筛选框功能
        $('.btn-sm').on('click', function (e) {
            e.preventDefault()
            // let type =$('#selCategory').val()
            // let state =$('#selStatus').val()
            // console.log($('#selCategory').val());
            // console.log($('#selStatus').val());
            // loadData(type,state)
            // 把上面loadData加两个参数

            loadData()
        })
    })
</script>